package com.zh.hadoop.mapreduce;

import java.io.IOException;
import java.util.StringTokenizer;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

/**
 * MapReduce
 */
public class WordCount {

	// step 1 Map Class
	public static class WordCountMapper extends
			Mapper<LongWritable, Text, Text, IntWritable> {

		private Text mapOutputKey = new Text();
		private final static IntWritable mapOutputValue = new IntWritable(1);

		@Override
		protected void map(LongWritable key, Text value, Context context)
				throws IOException, InterruptedException {

			// line value
			String lineValue = value.toString();
			StringTokenizer stringTokenizer = new StringTokenizer(lineValue);
			// iterator
			while (stringTokenizer.hasMoreTokens()) {
				// get word value
				String wordValue = stringTokenizer.nextToken();
				//
				mapOutputKey.set(wordValue);
				context.write(mapOutputKey, mapOutputValue);
			}
		}

	}

	// step 2 Reduce Class
	public static class WordCountReducer extends
			Reducer<Text, IntWritable, Text, IntWritable> {
		private IntWritable outputValue = new IntWritable();
		@Override
		protected void reduce(Text key, Iterable<IntWritable> values,
				Context context) throws IOException, InterruptedException {
			int sum = 0;
			// iterator
			for (IntWritable value : values) {
				// total
				sum += value.get();
			}
			// set value
			outputValue.set(sum);
			// output
			context.write(key, outputValue);
		}
	}

	// step 3 Driver ,componet job
	public int run(String[] args) {
		Configuration configuration = new Configuration();
		try {
			// 1.create job
			Job job = Job.getInstance(configuration, this.getClass()
					.getSimpleName());
			// run jar
			job.setJarByClass(this.getClass());
			// 3.set job
			// input ->map -> reduce ->output
			// 3.1 input
			Path inputPath = new Path(args[0]);
			FileInputFormat.addInputPath(job, inputPath);
			// 3.2 map
			job.setMapperClass(WordCountMapper.class);
			job.setOutputKeyClass(Text.class);
			job.setOutputValueClass(IntWritable.class);
			// 3.3 reduce
			job.setReducerClass(WordCountReducer.class);
			job.setOutputKeyClass(Text.class);
			job.setOutputValueClass(IntWritable.class);
			// 3.4 output
			Path outPath = new Path(args[1]);
			FileOutputFormat.setOutputPath(job, outPath);
			// 4.submit job
			Boolean isSuccess = job.waitForCompletion(true);
			return isSuccess ? 0 : 1;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 0;
	}

	@SuppressWarnings("unused")
	public static void main(String[] args) {
		args = new String[2];
		args[0] = "/mapreduce/input";
		args[1] = "/mapreduce/output1";
		int status = new WordCount().run(args);
		System.exit(0);
	}
}
